package com.infolab.aims.web;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.joda.time.LocalTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.infolab.aims.common.AbstractController;
import com.infolab.aims.common.bean.SearchCriteria;
import com.infolab.aims.domain.Report;
import com.infolab.aims.domain.Section;
import com.infolab.aims.domain.SystemUser;
import com.infolab.aims.service.intf.ReportService;
import com.infolab.aims.service.intf.SystemCodeService;
import com.infolab.aims.service.intf.SystemUserService;


@Controller
@RequestMapping("/ajax")
public class AjaxController extends AbstractController {
	
	@Autowired
	SystemUserService systemUserService;
	
	@Autowired
	SystemCodeService systemCodeService;
	
	@Autowired
	ReportService reportService;
	
	public final Integer MAX_ROW = 100;
	
	Log logger = LogFactory.getLog(getClass());
	
	@RequestMapping(method=RequestMethod.GET)
	public @ResponseBody String notFound()
	{
		return "[]";
	}
	
	@RequestMapping(value="section/{departmentId}", method = RequestMethod.GET)
	public @ResponseBody List<Section> showSection(@PathVariable String departmentId) {
		if(StringUtils.isNumeric(departmentId))
		{
			List list = commonService.getSection(Integer.valueOf(departmentId));
			return list ;
		}
		else
			return new ArrayList<Section>();
	}
	
	@RequestMapping(value="role/{sectionId}", method = RequestMethod.GET)
	public @ResponseBody List<Section> showRole(@PathVariable String sectionId) {
		if(StringUtils.isNumeric(sectionId))
		{
			List list = commonService.getRole(Integer.valueOf(sectionId));
			return list ;
		}
		else
			return new ArrayList<Section>();
		
	}
	
	
	
	@RequestMapping(value="technician", method = RequestMethod.GET)
	public @ResponseBody List<SystemUser> showTechnician(@RequestParam(value="q") String technicianNric) {
		
		SearchCriteria searchCriteria = new SearchCriteria();
		searchCriteria.addSearchCriteria("likeNric", technicianNric);
		searchCriteria.addSearchCriteria("userType", SystemUser.USERTYPE_SECURITY_GUARD);
		searchCriteria.setSortType(SearchCriteria.SORTTYPE_ASC);
		searchCriteria.setSortName("nric");
		searchCriteria.setMaxRow(MAX_ROW);
		List<SystemUser> list = systemUserService.findUsers(searchCriteria);
		
		List<SystemUser> compact = new ArrayList<SystemUser>();
		if(list!=null)
		{
			for(int i=0;i<list.size();i++)
			{
				SystemUser tmp = list.get(i);
				SystemUser systemUser = new SystemUser();
				systemUser.setPersonName(tmp.getPersonName());
				systemUser.setNric(tmp.getNric());
				systemUser.setUserName(tmp.getUserName());
				
				compact.add(systemUser);
			}
		}
		
		return compact;
	}
	
	
	
	@RequestMapping(value="currentTime", method = RequestMethod.GET)
	public @ResponseBody String showCurrentTime() {
		
		DateTimeFormatter fmt = DateTimeFormat.forPattern("HH:mm:ss");
		return new LocalTime().toString(fmt);
	}
	
	@RequestMapping(value="showReportProcessingStatus/{reportId}", method = RequestMethod.GET)
	public @ResponseBody String showReportProcessingStatus(@PathVariable String reportId) {
		
		Report report = reportService.getReport(reportId);
		if(report!=null)
			return report.getReportStatus();

		return Report.REPORTSTATUS_FAIL;
	}
	
}
